<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>
      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button
          v-for="(item, index) in butData"
          :key="index"
          :type="item.type"
          @click="item.do"
          v-auth="item.auth"
        >
          <Icon :icon="item.icon" /> {{ item.title }}
        </a-button>
      </template>
      <!-- 表格插槽 -->

      <template #jcjy="{ record }">
        <a style="display: flex; line-height: 30px" @click="handleForm({ id: record.id })">
          <Icon
            icon="ant-design:fast-forward-outlined"
            style="margin-right: 10px; font-size: 30px"
          />
          检验明细
        </a>
      </template>

      <!-- 表格插槽 -->
      <template #picno="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.picno }}
        </a>
      </template>
      <template #bhard="{ record, column }">
       
       <a-checkbox :checked="record[column.dataIndex] == 1 ? true : false" />
     </template>
    </BasicTable>
    <!-- 页签方式编辑 -->
    <InputFormTabs @register="registerDrawer2" @success="handleSuccess" />

    <SxForm @register="register4" @success="handleSuccess" />

    <Modal @register="register" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8OrderSxList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref, watch } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable, ActionItem } from '/@/components/Table';
  import { listSet, customListData } from '/@/api/test/testData';

  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputFormTabs from './formTabs.vue';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { useModal } from '/@/components/Modal';

  import SxForm from '/@/views/m8/order/sx/sxForm.vue';
  import { orderSxDelete } from '/@/api/bas/m8OrderRoute';
  import Modal from './Modal.vue';
  import { handleExport } from '/@/utils/export';

  const { showMessage } = useMessage();

  let routeData = ref<any>();
  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度
    showAdvancedButton: true,
    schemas: [],
  });
  let tableColumns = ref<any>([]);
  let butData = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title,
  });
  let title = ref('');

  watch(
    () => props.orderPic,
    () => {
      reload();
    },
  );

  const actionColumn: BasicColumn = {
    width: 150, //操作按钮宽度
    actions: (record: Recordable) => {
      actionColumnData.value.forEach((item) => {
        if (item.title == '编辑') {
          auth: 'm8:order:sx:edit';
          item.onClick = () => openDrawer4(true, { id: record.orderPic.id });
        }
        if (item.title == '删除') {
          item.popConfirm = {
            title: '是否确认删除数据',

            confirm: handleDelete.bind(this, { id: record.id }),
          };
          auth: 'm8:order:sx:edit';
        }
      });
      return actionColumnData.value;
    },
  };

  const props = defineProps({
    data: { type: Object, default: {} },
    orderPic: { type: String, default: '' },
  });
  onMounted(async () => {
    const res = await listSet({ viewCode: 'M8_order_sx_list' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);

    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头

    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    butData.value.forEach((item) => {
      if (item.title == '指定单价') {
        auth: 'm8:order:sx:edit';
        item.do = () => btn1();
      }
      if (item.btnKey == 'btnExport') {
        item.do = () => btnExport();
      }
    });

    tableColumns.value.forEach((item) => {
      if (item.title == '顺序号') {
        item.fixed = 'left';
      } else if (item.title == '数铣状态') {
        item.fixed = 'right';
      }
    });

    actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮

    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    setProps({
      api: customListData,
      beforeFetch: (params) => {
        params.url = listTabConfig.value.api;
        params['order.id'] = props.data.id;
        params['orderPic.id'] = props.orderPic;
        return params;
      },
      autoCreateKey: listTabConfig.value.autoCreateKey,
      bordered: listTabConfig.value.bordered,
      canResize: listTabConfig.value.canResize,
      clearSelectOnPageChange: listTabConfig.value.clearSelectOnPageChange,
      clickToRowSelect: listTabConfig.value.clickToRowSelect,
      ellipsis: listTabConfig.value.ellipsis,
      emptyDataIsShowTable: listTabConfig.value.emptyDataIsShowTable,
      inset: listTabConfig.value.inset,
      isTreeTable: listTabConfig.value.isTreeTable,
      pagination: listTabConfig.value.pagination,
      showIndexColumn: listTabConfig.value.showIndexColumn,
      showSummary: listTabConfig.value.showSummary,
      showTableSetting: listTabConfig.value.showTableSetting,
      striped: listTabConfig.value.striped,
      useSearchForm: listTabConfig.value.useSearchForm,
      tableSetting: listTabConfig.value.tableSetting,
      titleHelpMessage: listTabConfig.value.titleHelpMessage,
      maxHeight: listTabConfig.value.maxHeight,
      resizeHeightOffset: listTabConfig.value.resizeHeightOffset,
    });
    if (!listTabConfig.value.immediate) {
      reload();
    }
  });
  const [register, { openModal }] = useModal();
  const [register4, { openDrawer: openDrawer4 }] = useDrawer();

  const [registerDrawer2, { openDrawer: openDrawer2 }] = useDrawer();
  const [registerTable, { reload, getForm, setProps, getSelectRows }] = useTable({
    actionColumn: actionColumn,
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
    rowSelection: {
      type: 'checkbox',
    },
    // 不显示序列号
    showIndexColumn: false,
  });

  function btn1() {
    if (getSelectRows().length == 1) {
      openModal(true, { ...getSelectRows()[0], title: '指定单价' });
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  // 刷新表格数据（含表单回调）
  function handleSuccess() {
    reload();
  }
  defineExpose({
    handleSuccess,
  });

  function handleForm(record: Recordable) {
    openDrawer2(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await orderSxDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  //导出数据
  function btnExport() {
    var formdata = getForm().getFieldsValue();
    formdata = { ...formdata, 'order.id': props.data.id };
    handleExport('/m8/order/sx/exportData', formdata);
  }
</script>
<style>
  .jeesite-editable-cell__action {
    display: none;
  }
  .edit-cell-align-center {
    width: 100% !important;
  }
  #ifrId {
    width: 100%;
    height: 800px;
  }
</style>
